package com.ruoyi.data.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.data.domain.QualityControlVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.data.mapper.BusWaterMeteorologicaldataMapper;
import com.ruoyi.data.domain.BusWaterMeteorologicaldata;
import com.ruoyi.data.service.IBusWaterMeteorologicaldataService;

/**
 * 气象数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-21
 */
@Service
public class BusWaterMeteorologicaldataServiceImpl implements IBusWaterMeteorologicaldataService 
{
    @Autowired
    private BusWaterMeteorologicaldataMapper busWaterMeteorologicaldataMapper;

    /**
     * 查询气象数据
     * 
     * @param locationid 气象数据主键
     * @return 气象数据
     */
    @Override
    public BusWaterMeteorologicaldata selectBusWaterMeteorologicaldataByLocationid(String locationid)
    {
        return busWaterMeteorologicaldataMapper.selectBusWaterMeteorologicaldataByLocationid(locationid);
    }

    /**
     * 查询气象数据列表
     * 
     * @param busWaterMeteorologicaldata 气象数据
     * @return 气象数据
     */
    @Override
    public List<BusWaterMeteorologicaldata> selectBusWaterMeteorologicaldataList(BusWaterMeteorologicaldata busWaterMeteorologicaldata)
    {
        return busWaterMeteorologicaldataMapper.selectBusWaterMeteorologicaldataList(busWaterMeteorologicaldata);
    }

    /**
     * 新增气象数据
     * 
     * @param busWaterMeteorologicaldata 气象数据
     * @return 结果
     */
    @Override
    public int insertBusWaterMeteorologicaldata(BusWaterMeteorologicaldata busWaterMeteorologicaldata)
    {
        return busWaterMeteorologicaldataMapper.insertBusWaterMeteorologicaldata(busWaterMeteorologicaldata);
    }

    /**
     * 修改气象数据
     * 
     * @param busWaterMeteorologicaldata 气象数据
     * @return 结果
     */
    @Override
    public int updateBusWaterMeteorologicaldata(BusWaterMeteorologicaldata busWaterMeteorologicaldata)
    {
        return busWaterMeteorologicaldataMapper.updateBusWaterMeteorologicaldata(busWaterMeteorologicaldata);
    }

    /**
     * 批量删除气象数据
     * 
     * @param locationids 需要删除的气象数据主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterMeteorologicaldataByLocationids(String[] locationids)
    {
        return busWaterMeteorologicaldataMapper.deleteBusWaterMeteorologicaldataByLocationids(locationids);
    }

    /**
     * 删除气象数据信息
     * 
     * @param locationid 气象数据主键
     * @return 结果
     */
    @Override
    public int deleteBusWaterMeteorologicaldataByLocationid(String locationid)
    {
        return busWaterMeteorologicaldataMapper.deleteBusWaterMeteorologicaldataByLocationid(locationid);
    }

    @Override
    public List<Map<String, Object>> getQualityControlData(QualityControlVo vo) {
        List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
        DecimalFormat df = new DecimalFormat("#.##");
        //获取时间段内应上传的数据，每小时一条数据
        if (StringUtils.isNotEmpty(vo.getBeginTime()) && StringUtils.isNotEmpty(vo.getEndTime())) {
            String beginTime =vo.getBeginTime();
            String endTime = vo.getEndTime();
            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            // 解析字符串为LocalDate对象
            LocalDate startDate = LocalDate.parse(beginTime, formatter);
            LocalDate endDate = LocalDate.parse(endTime, formatter);
            // 计算两个日期之间的天数
            long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);

            vo.setBeginTime(vo.getBeginTime() + " 00:00:00");
            vo.setEndTime(vo.getEndTime() + " 23:59:59");

            List<Map<String,Object>> list = busWaterMeteorologicaldataMapper.getQualityControlData(vo);

            //根据断面名称分组判断，stream流
            Map<String, List<Map<String, Object>>> groupBySegment =
                    list.stream().collect(
                            Collectors.groupingBy(item -> item.get("SegmentName").toString())
                    );
            for (String segment : groupBySegment.keySet()) {
                Map<String, Object> temp = new HashMap<String, Object>();
                temp.put("SegmentName", segment);
                //遍历断面下数据，进行传输率，有效率计算
                List<Map<String, Object>> list1 = groupBySegment.get(segment);
                if (list1 != null && !list1.isEmpty()) {
                    //总合格数
                    double sumYXL = 0.00;
                    //总合格率
                    double sumCXL = 0.00;

                    for (Map<String, Object> map1: list1) {
                        // 24小时*天数=应上传数据量
                        long allNumData =(daysBetween +1) * 24;
                        //实际上传数据量
                        long uploadNum = (long) map1.get("totalCount");
                        //long uploadNum = (long) map1.get("hourTotalCount");
                        //常规五参，每小时一条，其他因子为整4点数据
                        if(!"水温".equals(map1.get("ItemName").toString()) && !"pH".equals(map1.get("ItemName").toString()) && !"溶解氧".equals(map1.get("ItemName").toString()) && !"浑浊度".equals(map1.get("ItemName").toString()) && !"电导率".equals(map1.get("ItemName").toString())){
                            allNumData = allNumData / 4;
                        }
                        // 应上传数据量
                        map1.put("oughtToNum",allNumData);
                        // 合格数据量
                        long receiveNum = 0;
                        Object obj = map1.get("standardCount");
                        if (obj instanceof BigDecimal) {
                            BigDecimal bigDecimal = (BigDecimal) obj;
                            receiveNum = bigDecimal.longValue(); // 使用 longValue() 方法进行转换
                        } else {
                            // 处理错误情况或进行其他逻辑
                            // 有效数据量
                            receiveNum = (long)obj;
                        }
                        if (0 != uploadNum ){
                            // 合格率
                            double efficiency = (double) receiveNum / uploadNum * 100;
                            sumYXL += efficiency;
                            map1.put("Efficiency", df.format(efficiency));
                        }else {
                            map1.put("Efficiency", "-");
                        }

                    }
                    //因子数据列表
                    temp.put("itemList", list1);
                    retList.add(temp);
                }
            }
        }

        return retList;
    }
}
